#include "timer.h"

#define RET_OK 0
#define RET_FAIL 1

uint32_t timerExec;

static unsigned int lastTickCount = 0;
static unsigned int led_Level = 0;

static unsigned int operation_begin = 0;
static unsigned int operation_plus = 0;

static char time[] ="00:00";

static unsigned int Press_PlusOK = 1;
static unsigned int Press_BeginOK = 0;


/**
 * @brief 倒计时 
 * 
 */
void CountDown(void *arg)
{
    printf("CountDown!\r\n");
    //uint32_t t = (uint32_t)arg;
    led_Level ^= 1;
    IoTGpioSetOutputVal(IOT_GPIO_IO_GPIO_9, led_Level);
    time[0]='0';
    time[1]='0';
    time[3]='0';
    time[4]='0';
    printf("%s\r\n",time);
    Press_PlusOK=1;
    
}

/**
 * @brief 倒计时开始
 * 
 */
void CountDownStart(void)
{
    while(1)
    {
        usleep(15000);
        if(operation_begin == 1)
    {
        //初始化主板灯引脚
        IoTGpioInit(IOT_GPIO_IO_GPIO_9);
        IoTIoSetFunc(IOT_GPIO_IO_GPIO_9, IOT_IO_FUNC_GPIO_9_GPIO);
        IoTGpioSetDir(IOT_GPIO_IO_GPIO_9, IOT_GPIO_DIR_OUT);
        IoTGpioSetOutputVal(IOT_GPIO_IO_GPIO_9, IOT_GPIO_VALUE0);

        printf("operation_begin ! \r\n");
        osTimerId_t timerId;
        osStatus_t status;

        timerExec = 1U;
        printf("Pre timer create! \r\n");
        timerId = osTimerNew((osTimerFunc_t)CountDown,osTimerOnce, &timerExec, NULL);
        if(timerId != NULL)
        {
            uint32_t delay;
            printf("timer create\r\n ");
            delay = (int)(((time[4]-'0')+(time[3]-'0')*10+(time[1]-'0')*60+(time[0]-'0')*600)*100);
            printf("delay = %u \r\n",delay);
            //delay = 500U;
            status = osTimerStart(timerId,delay);
            if(status != osOK)
            {
                printf("Falied to start timer! \r\n");
            }
        }
        else
        {
            printf("Falied to create timer!\r\n");
        }
        operation_begin = 0;
    }
     
 } 
}



/**
 * @brief 加时 
 * 
 */
void OperationPlus(void)
{
    printf("OperationPlus \r\n");
    if(time[4] == '9')
    {
        time[3] += 1;
        time[4] = '0';
    }
    else
    {
        time[4] += 1;
    }
    if(time[3] == '6')
    {
        time[1] += 1;
        time[3] = '0';
    }
    if(time[1] == '9')
    {
        if(time[0] == '9')
        {
            time[4] = '0';
            time[3] = '0';
            time[1] = '0';
            time[0] = '0';
        }
        else
        {
            time[0] += 1;
            time[1] = '0';
        }
       
    }
}


/**
 * @brief 操作判断
 */
void OperationCheck(void)
{
    /*if(operation_begin == 1)
    {
       
    }*/
     if(operation_plus == 1)
    {
        printf("OperationCheck operation_plus = %u \r\n",operation_plus);
        OperationPlus();
        operation_plus = 0;
    }
}

void GpioTimerPressedIsrFunc(char *arg)
{
    (void)arg;
    
    //电压控制
    float voltage;
    unsigned int ret;
    unsigned short val;
    
     //消除按键抖动
    unsigned int tickCount = osKernelGetTickCount();
    unsigned int count = tickCount - lastTickCount;
    lastTickCount = tickCount;

     //获取ADC采集值
    ret = IoTAdcRead(IOT_ADC_CHANNEL_2, &val, IOT_ADC_EQU_MODEL_8, IOT_ADC_CUR_BAIS_DEFAULT, 256);

    if (ret != EC_SUCCESS)
    {
        printf("Failed to read adc! Err code = %u\n", ret);
    }
	
    //将ADC采集值转换成电压值
    voltage = IOTAdcConvertToVoltage(val);

    if(count>50)
    {
        if(voltage<1) //减
        {
          operation_begin = 1;
          operation_plus = 0;
        }
        else if(voltage>=1&&voltage<2)  //加
        {
           operation_plus = 1;
           operation_begin = 0;
            OperationCheck();
          Press_PlusOK = 1;
          
        }
    }
    printf("voltge:%.2f \r\n",voltage);
    printf(" operation_plus = %u \r\n",operation_plus);
    printf(" operation_begin = %u \r\n", operation_begin);
    printf("Press_PlusOK = %u \r\n",Press_PlusOK);
    printf("time = %s \r\n",time);
	
   
}


/**
 * @brief  定时时间加减按键
 */
 void *OledTimerpressTask(const char *arg)
{
    (void)arg;

    unsigned int interrupt;
    //初始化GPIO5引脚
    IoTGpioInit(IOT_GPIO_IO_GPIO_5);
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_5, IOT_IO_FUNC_GPIO_5_GPIO);
    IoTIoSetPull(IOT_GPIO_IO_GPIO_5, IOT_IO_PULL_UP);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_5, IOT_GPIO_DIR_IN);


    //设置GPIO5中断
    interrupt=IoTGpioRegisterIsrFunc(IOT_GPIO_IO_GPIO_5, IOT_INT_TYPE_EDGE,
                                 IOT_GPIO_EDGE_FALL_LEVEL_LOW, GpioTimerPressedIsrFunc, NULL);
    if (interrupt != RET_OK)
    {
        printf("===== ERROR ======gpio -> hi_gpio_register_isr_function ret:%u\r\n", interrupt);
    }
	

    return NULL;
}

/**
 * @brief Oled主任务函数
 */ 
 void TimerTask(void)
{
   
    while(1)
    {
        usleep(15000);
        if(Press_PlusOK == 1)
        {
             //初始化I2C0 配置，速率为400K
	        I2coInit();
            //oled初始化配置
            OledInit();
            printf("TimerTask \r \n");
	        //显示字符串
            OledShowString(40,30,time,16);
	        OledRefresh();
            Press_PlusOK = 0;
	        //删除任务
	        //osThreadTerminate(g_oledTaskID);
        }
    }
}
